/*
 * NewRandomGraphPanel.java
 * Created on Jul 20, 2010, 3:32:51 PM
 */

package org.bm.graphexplorer.components;

import org.bm.blaise.scio.graph.Graph;
import org.bm.blaise.scio.graph.PreferentialAttachment;
import org.bm.blaise.scio.graph.RandomGraph;

/**
 *
 * @author elisha
 */
public class NewPreferentialGraphPanel extends javax.swing.JPanel {


    /** Creates new form NewRandomGraphPanel */
    public NewPreferentialGraphPanel() {
        initComponents();
    }

    /** @return graph corresponding to the current graph settings */
    public Object getInstance() {
        boolean directed = basicP.getDirected();
        int order = basicP.getOrder();
        
        int seedOrder = (Integer) seedNodesS.getValue();
        float seedProb = (Float) seedProbS.getValue();
        int seedEdges = (Integer) seedEdgesS.getValue();        
        boolean seedByMumber = numRB.isSelected();
        Graph<Integer> seed = seedByMumber ? RandomGraph.getInstance(seedOrder, seedEdges, directed)
                : RandomGraph.getInstance(seedOrder, seedProb, directed);

        boolean fixedAdd = fixedRB.isSelected();
        boolean returnLongitudinal = returnLongRB.isSelected();

        if (fixedAdd) {
            int perStep = (Integer) fixedStepS.getValue();
            return returnLongitudinal ? PreferentialAttachment.getLongitudinalSeededInstance(seed, order, perStep)
                    : PreferentialAttachment.getSeededInstance(seed, order, perStep);
        } else {
            String[] responseArr = variableTF.getText().split(",");
            float[] probs = new float[responseArr.length];
            try {
                float sum = 0f;
                for (int i = 0; i < responseArr.length; i++) {
                    probs[i] = Float.valueOf(responseArr[i]);
                    sum += probs[i];
                }
                if (sum != 1f)
                    throw new IllegalArgumentException("Probabilities do not sum to 1!");
                return returnLongitudinal ? PreferentialAttachment.getTimeSeededInstance(seed, order, probs)
                        : PreferentialAttachment.getSeededInstance(seed, order, probs);
            } catch (NumberFormatException ex) { System.out.println("Improperly formatted number..."); }
        }
        return null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        edgeTypeBG = new javax.swing.ButtonGroup();
        directedBG = new javax.swing.ButtonGroup();
        resultBG = new javax.swing.ButtonGroup();
        stepBG = new javax.swing.ButtonGroup();
        basicP = new NewSimpleGraphPanel(100, 100000, true);
        jPanel1 = new javax.swing.JPanel();
        numRB = new javax.swing.JRadioButton();
        seedEdgesS = new javax.swing.JSpinner();
        seedProbS = new javax.swing.JSpinner();
        probRB = new javax.swing.JRadioButton();
        seedNodesS = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        fixedStepS = new javax.swing.JSpinner();
        fixedRB = new javax.swing.JRadioButton();
        variableRB = new javax.swing.JRadioButton();
        variableTF = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        returnRegularRB = new javax.swing.JRadioButton();
        returnLongRB = new javax.swing.JRadioButton();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));
        add(basicP);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Seed Graph"));
        jPanel1.setToolTipText("<html>\nThe <b>seed graph</b> will be used as the starting point\n<br>\nfor the construction of the preferential attachment graph.\n<br>\nIdeally, it should contain a relatively small number of vertices.");

        edgeTypeBG.add(numRB);
        numRB.setSelected(true);
        numRB.setText("seed # of edges:");

        seedEdgesS.setModel(new javax.swing.SpinnerNumberModel(10, 0, 100000, 1));
        seedEdgesS.setToolTipText("Enter the number of edges in the seed graph (0-100,000)");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, numRB, org.jdesktop.beansbinding.ELProperty.create("${selected}"), seedEdgesS, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        seedProbS.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(0.1f), Float.valueOf(0.0f), Float.valueOf(1.0f), Float.valueOf(0.1f)));
        seedProbS.setToolTipText("Enter the probability of occurence for each edge in the seed graph (0.00-1.00)");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, probRB, org.jdesktop.beansbinding.ELProperty.create("${selected}"), seedProbS, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        edgeTypeBG.add(probRB);
        probRB.setText("probability per edge:");

        seedNodesS.setModel(new javax.swing.SpinnerNumberModel(10, 1, 100000, 1));
        seedNodesS.setToolTipText("Enter the number of nodes in the seed graph (1-100,000)");

        jLabel1.setText("Seed # nodes:");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(13, 13, 13)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(seedNodesS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(probRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(seedProbS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 68, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(numRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(seedEdgesS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(154, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(seedNodesS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(probRB)
                    .add(seedProbS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(numRB)
                    .add(seedEdgesS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        add(jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Edges per Step"));

        fixedStepS.setModel(new javax.swing.SpinnerNumberModel(1, 1, 1000, 1));
        fixedStepS.setToolTipText("Enter the number of nodes in the resulting graph (1-100,000)");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, fixedRB, org.jdesktop.beansbinding.ELProperty.create("${selected}"), fixedStepS, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        stepBG.add(fixedRB);
        fixedRB.setSelected(true);
        fixedRB.setText("Fixed");

        stepBG.add(variableRB);
        variableRB.setText("Variable");

        variableTF.setText("0,.5,.5");
        variableTF.setToolTipText("<html>Enter comma-based list of probabilities summing to 1.<br>The first entry is the probability of adding 0 edges in a step,<br>the second is the probability of adding 1 edges, etc.");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, variableRB, org.jdesktop.beansbinding.ELProperty.create("${selected}"), variableTF, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(variableRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(variableTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(fixedRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(fixedStepS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(214, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(fixedRB)
                    .add(fixedStepS, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(variableRB)
                    .add(variableTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel2);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Result Type"));

        resultBG.add(returnRegularRB);
        returnRegularRB.setSelected(true);
        returnRegularRB.setText("regular graph");
        jPanel3.add(returnRegularRB);

        resultBG.add(returnLongRB);
        returnLongRB.setText("longitudinal graph");
        jPanel3.add(returnLongRB);

        add(jPanel3);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.bm.graphexplorer.components.NewSimpleGraphPanel basicP;
    private javax.swing.ButtonGroup directedBG;
    private javax.swing.ButtonGroup edgeTypeBG;
    private javax.swing.JRadioButton fixedRB;
    private javax.swing.JSpinner fixedStepS;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton numRB;
    private javax.swing.JRadioButton probRB;
    private javax.swing.ButtonGroup resultBG;
    private javax.swing.JRadioButton returnLongRB;
    private javax.swing.JRadioButton returnRegularRB;
    private javax.swing.JSpinner seedEdgesS;
    private javax.swing.JSpinner seedNodesS;
    private javax.swing.JSpinner seedProbS;
    private javax.swing.ButtonGroup stepBG;
    private javax.swing.JRadioButton variableRB;
    private javax.swing.JTextField variableTF;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
